home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc / Sample Code / Sample Editors⁄Viewers / Cappuccino / Source / CappuccinoPromise.cpp < prev    next >
Encoding:
Text File  |  1995-12-11  |  9.0 KB  |  347 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        CappuccinoPromise.cpp
  3.     
  4.     Contents:    Classes to encapsulate promises.
  5.     
  6.     Written by:    Troy Gaul
  7.     
  8.     Copyright:    © 1995 by Apple Computer, Inc., all rights reserved.
  9. */
  10.  
  11. // -- Compiler/Preprocessor Switches --
  12.  
  13. #ifndef _COMPILERDEFS_
  14. #include "CompDefs.h"
  15. #endif
  16.  
  17. // -- OpenDoc Utilities --
  18.  
  19. #ifndef _EXCEPT_
  20. // Exceptions define several important macros (eg. CHECKENV)
  21. // which are used in the SOM method dispatch glue. If Except.h
  22. // is not included early enough, exceptions may not be thrown
  23. // correctly when returning from a SOM method with the "ev" parameter set.
  24. #include <Except.h>
  25. #endif
  26.  
  27. // -- Cappuccino Includes --
  28.  
  29. #ifndef _CAPPUCCINOPROMISE_
  30. #include "CappuccinoPromise.h"
  31. #endif
  32.  
  33. #ifndef _CAPPUCCINO_
  34. #include "Cappuccino.h"
  35. #endif
  36.  
  37. #ifndef _CAPPUCCINOCONTENT_
  38. #include "CappuccinoContent.h"
  39. #endif
  40.  
  41. #ifndef _CAPPUCCINODEF_
  42. #include "CappuccinoDef.h"
  43. #endif
  44.  
  45. #ifndef _CAPPUCCINOGLOBALS_
  46. #include "CappuccinoGlobals.h"
  47. #endif
  48.  
  49. #ifndef _SAMPLECOLLECTIONS_
  50. #include "SampleCollections.h"
  51. #endif
  52.  
  53. // -- OpenDoc Includes --
  54.  
  55. #ifndef _ODTYPES_
  56. #include <ODTypes.h>
  57. #endif
  58.  
  59. #ifndef SOM_Module_OpenDoc_StdProps_defined
  60. #include <StdProps.xh>
  61. #endif
  62.  
  63. #ifndef SOM_ODStorageUnit_xh
  64. #include <StorageU.xh>
  65. #endif
  66.  
  67. // -- OpenDoc Utilities --
  68.  
  69. #ifndef _BARRAY_
  70. #include <BArray.h>
  71. #endif
  72.  
  73. #ifndef _ODDEBUG_
  74. #include <ODDebug.h>
  75. #endif
  76.  
  77. #ifndef _ODUTILS_
  78. #include <ODUtils.h>
  79. #endif
  80.  
  81. #ifndef _TEMPOBJ_
  82. #include <TempObj.h>
  83. #endif
  84.  
  85.  
  86. //------------------------------------------------------------------------------
  87. // SCappuccinoPromiseData
  88. //------------------------------------------------------------------------------
  89.  
  90. struct SCappuccinoPromiseData
  91. {
  92.     CPromise*        fPromise;
  93. };
  94.  
  95. //==============================================================================
  96. // CPromiseSet
  97. //==============================================================================
  98. #pragma mark • CPromiseSet •
  99.  
  100. //------------------------------------------------------------------------------
  101. // Function:    Constructor
  102. // Origin:        CPromiseSet
  103. //------------------------------------------------------------------------------
  104.  
  105. CPromiseSet::CPromiseSet()
  106. {
  107.     fPromiseList = kODNULL;
  108.     
  109.     fPromiseList = new CList;
  110. }
  111.  
  112. //------------------------------------------------------------------------------
  113. // Function:    Destructor
  114. // Origin:        CPromiseSet
  115. //------------------------------------------------------------------------------
  116.  
  117. CPromiseSet::~CPromiseSet()
  118. {
  119.     // Remove and release our promise objects.
  120.     if ( fPromiseList )
  121.     {
  122.         Environment* ev = somGetGlobalEnvironment();
  123.         
  124.         TRY
  125.             this->Clear(ev);
  126.         CATCH_ALL
  127.         ENDTRY
  128.         
  129.         ODDeleteObject(fPromiseList);
  130.     }
  131. }
  132.  
  133. //------------------------------------------------------------------------------
  134. // Function:    Clear
  135. // Origin:        CPromiseSet
  136. //------------------------------------------------------------------------------
  137.  
  138. void CPromiseSet::Clear( Environment* ev )
  139. {
  140.     CListIterator fiter(fPromiseList);
  141.     for ( CPromise* promise = (CPromise*) fiter.First();
  142.             fiter.IsNotComplete(); promise = (CPromise*) fiter.Next() )
  143.     {
  144.         fiter.RemoveCurrent();
  145.         delete promise;
  146.     }
  147. }
  148.  
  149. //------------------------------------------------------------------------------
  150. // Function:    Add
  151. // Origin:        CPromiseSet
  152. //------------------------------------------------------------------------------
  153.  
  154. void CPromiseSet::Add( Environment*        ev, 
  155.                        CPromise*        promise )
  156. {
  157.     ASSERT_NOT_NULL(promise);
  158.     fPromiseList->Add(promise);
  159. }
  160.  
  161. //==============================================================================
  162. // CPromise
  163. //==============================================================================
  164. #pragma mark • CPromise •
  165.  
  166. //------------------------------------------------------------------------------
  167. // Function:    Constructor
  168. // Origin:        CPromise
  169. //------------------------------------------------------------------------------
  170.  
  171. CPromise::CPromise( Cappuccino* part )
  172. {
  173.     fPart = part;
  174.     fIsClipboardPromise = kODFalse;
  175.     fUpdateID = kODNULLID;
  176.     fFulfilled = kODFalse;
  177. }
  178.  
  179. //------------------------------------------------------------------------------
  180. // Function:    Destructor
  181. // Origin:        CPromise
  182. //------------------------------------------------------------------------------
  183.  
  184. CPromise::~CPromise()
  185. {
  186. }
  187.  
  188. //------------------------------------------------------------------------------
  189. // Function:    PromiseTo
  190. // Origin:        CPromise
  191. //------------------------------------------------------------------------------
  192.  
  193. void CPromise::PromiseTo( Environment*        ev, 
  194.                           ODStorageUnit*    su)
  195. {
  196. }
  197.  
  198. //------------------------------------------------------------------------------
  199. // Function:    Fulfill
  200. // Origin:        CPromise
  201. //------------------------------------------------------------------------------
  202.  
  203. void CPromise::Fulfill( Environment*        ev,
  204.                         ODStorageUnitView*    promiseSUView )
  205. {
  206.     WASSERT(!fFulfilled);
  207.  
  208.     fFulfilled = kODTrue;
  209. }
  210.  
  211. //------------------------------------------------------------------------------
  212. // Function:    CreateActionData
  213. // Origin:        CPromise
  214. //
  215. // Description:    This method is called when this promise must be encapsulated
  216. //                into a byte array.
  217. //------------------------------------------------------------------------------
  218.  
  219. ODByteArray* CPromise::CreatePromiseData()
  220. {
  221.     SCappuccinoPromiseData data;
  222.     data.fPromise = this;
  223.  
  224.     return CreateByteArray(&data, sizeof data);
  225. }
  226.  
  227. //------------------------------------------------------------------------------
  228. // Function:    GetPromiseFromByteArray                                [static]
  229. // Origin:        CPromise
  230. //
  231. // Description:    This method is called when ...
  232. //------------------------------------------------------------------------------
  233.  
  234. CPromise* CPromise::GetPromiseFromByteArray( ODByteArray* promise )
  235. {
  236.     ASSERT_NOT_NULL(promise);
  237.     ASSERT_NOT_NULL(promise->_buffer);
  238.     
  239.     return ((SCappuccinoPromiseData*) promise->_buffer)->fPromise;
  240. }
  241.  
  242. //------------------------------------------------------------------------------
  243. // Function:    GetPromiseFromSUView                                [static]
  244. // Origin:        CPromise
  245. //
  246. // Description:    This method is called when Fulfill promise has been called on
  247. //                a promised storage unit.
  248. //
  249. //                NOTE: This function will only work when this function is
  250. //                called in the handling of a call to the part's FulfillPromise
  251. //                method.
  252. //------------------------------------------------------------------------------
  253.  
  254. CPromise* CPromise::GetPromiseFromSUView( Environment*            ev,
  255.                                           ODStorageUnitView*    suView )
  256. {
  257.     ASSERT_NOT_NULL(suView);
  258.     
  259.     // Since this is made to be used by Fulfill promise, the flag indicating
  260.     // that this is a promise value should have been removed by now (by
  261.     // OpenDoc before calling ODPart::FulfillPromise to prevent recursion).
  262.     // If this were still marked as a promise value, the GetValue call below
  263.     // would cause the promise to be fulfilled, which may cause an infinite
  264.     // recursion.
  265.     WASSERT(!suView->IsPromiseValue(ev));
  266.     
  267.     ODByteArray data;
  268.     suView->GetValue(ev, sizeof(SCappuccinoPromiseData), &data);
  269.     
  270.     return CPromise::GetPromiseFromByteArray(&data);
  271. }
  272.  
  273.  
  274. //==============================================================================
  275. // CCappuccinoContentPromise
  276. //==============================================================================
  277. #pragma mark • CCappuccinoContentPromise •
  278.  
  279. //------------------------------------------------------------------------------
  280. // Function:    Constructor
  281. // Origin:        CCappuccinoContentPromise
  282. //------------------------------------------------------------------------------
  283.  
  284. CCappuccinoContentPromise::CCappuccinoContentPromise( Cappuccino*            part, 
  285.                                                       CCappuccinoContent*    content,
  286.                                                       ODValueType            valueType )
  287.     : CPromise(part)
  288. {
  289.     ASSERT_NOT_NULL(content);
  290.     ASSERT_NOT_NULL(valueType);
  291.     
  292.     fContent = kODNULL;
  293.     fValueType = kODNULL;
  294.     
  295.     fContent = content;
  296.     fContent->Acquire();
  297.     
  298.     fValueType = valueType;
  299. }
  300.  
  301. //------------------------------------------------------------------------------
  302. // Function:    Destructor
  303. // Origin:        CCappuccinoContentPromise
  304. //------------------------------------------------------------------------------
  305.  
  306. CCappuccinoContentPromise::~CCappuccinoContentPromise()
  307. {
  308.     fContent->Release();
  309. }
  310.  
  311. //------------------------------------------------------------------------------
  312. // Function:    PromiseTo
  313. // Origin:        CCappuccinoContentPromise
  314. //------------------------------------------------------------------------------
  315.  
  316. void CCappuccinoContentPromise::PromiseTo( Environment*        ev,
  317.                                            ODStorageUnit*    su )
  318. {
  319.     ASSERT_NOT_NULL(su);
  320.  
  321.     TempODByteArray data = this->CreatePromiseData();
  322.     
  323.     su->Focus(ev, kODPropContents, kODPosUndefined, kODNULL, 0, kODPosAll);
  324.     su->SetPromiseValue(ev, fValueType, 0, data, fPart->GetODPart());
  325.     
  326.     Inherited::PromiseTo(ev, su);
  327. }
  328.  
  329. //------------------------------------------------------------------------------
  330. // Function:    Fulfill
  331. // Origin:        CCappuccinoContentPromise
  332. //------------------------------------------------------------------------------
  333.  
  334. void CCappuccinoContentPromise::Fulfill( Environment*            ev,
  335.                                          ODStorageUnitView*        promiseSUView )
  336. {
  337.     ASSERT_NOT_NULL(promiseSUView);
  338.  
  339.     TempODValueType valueType = promiseSUView->GetType(ev);
  340.     ODStorageUnit* su = promiseSUView->GetStorageUnit(ev);
  341.     
  342.     fContent->Fulfill(ev, su, valueType);
  343.     
  344.     Inherited::Fulfill(ev, promiseSUView);
  345. }
  346.  
  347.